﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class PhieuHangGiaCongDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();
        public static List<PHIEUHANGGIACONG> GetAllPhieuHangGiaCong()
        {
            return db.PHIEUHANGGIACONGs.ToList();
        }
        public static List<PHIEUHANGGIACONG> SelectPhieuHangGiaCong()
        {
            return db.PHIEUHANGGIACONGs.ToList();
        }
        public static List<PHIEUHANGGIACONG> SelectPhieuHangGiaCongByNgayThanhToan(PHIEUHANGGIACONG phgc)
        {
            var query = from d in db.PHIEUHANGGIACONGs
                        where d.NgayThanhToan == phgc.NgayThanhToan
                        select d;

            return query.ToList();
        }
        public static void Insert(PHIEUHANGGIACONG phgc)
        {            
            db.PHIEUHANGGIACONGs.Add(phgc);
            db.SaveChanges();
        }
        public static void Delete(PHIEUHANGGIACONG phgc)
        {
            var query=db.PHIEUHANGGIACONGs.Find(phgc.MaPhieuHangGiaCong);
            if (query != null)
            {
                db.PHIEUHANGGIACONGs.Remove(phgc);
                db.SaveChanges();
            }
        }

        public static int GetLastMaPhieuHangGiaCong()
        {
            var query = (from d in db.PHIEUHANGGIACONGs
                         select d.MaPhieuHangGiaCong).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
